define(function(require, exports, module) {
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var backDlg = require("resource/js/component/backDialog");
	var easyutil = require("resource/js/component/easyutil.js");
	var combobox = require("resource/js/component/easycombobox.js");
	var ajax = require("resource/js/component/ajax.js");
	combobox.create({
		url: BASEPATH + "/base/authoriza/basecode/getType.htm",
		param: {
			checkeds: "itemiskey,oa_power_type"
		}
	});
	var config = {
		type: "buss",
		name: "业务流程",
		column: [
			[{
				field: 'businessName',
				title: '业务名称',
				width: 100,
				align: 'center',
				height: 50
			},{
				field: 'moudel_type_text',
				title: '所属应用',
				width: 100,
				align: 'center',
				height: 50,
			},{
				field: 'processKey',
				title: '流程编码',
				width: 100,
				align: 'center',
				height: 50
			}, {
				field: 'processName',
				title: '流程名称',
				width: 100,
				align: 'center',
				height: 50
			},{
				field: 'msgTypeText',
				title: '通知消息类型',
				width: 70,
				align: 'center',
				height: 50
			}, {
				field: 'iconUrl',
				title: '手机图标',
				width: 70,
				align: 'center',
				height: 50,
				formatter: function(value, row, index) {
					if (value != null && value != '') {
						return "<img src='" + BASEPATH + value + "' style='width:30px;height:30px;'/>";
					} else {
						return '';
					}
				}
			}, {
				field: 'isDefined',
				title: '是否自定义流程',
				width: 70,
				align: 'center',
				height: 50,
				formatter: function(val) {
					return valFormatter(val);
				}
			}, {
				field: 'phoneStatus',
				title: 'APP是否显示',
				width: 70,
				align: 'center',
				height: 50,
				formatter: function(val) {
					return valFormatter(val);
				}
			}, {
				field: 'totalTimeLimit',
				title: '总时限(分钟)',
				width: 70,
				align: 'center',
				height: 50
			}]
		]
	};
	var valFormatter = function(val) {
		if (val == "0") {
			return '是';
		} else if (val == "1") {
			return '否';
		} else {
			return val;
		}
	}
	var dgutil = undefined;
	var datagridconfig = undefined;

	function initPage() {
		//初始化主表datagrid
		dgutil = new easyutil.ajaxDataGrid(datagridconfig);
	}
	var initUploadImg = function(id, arg) {
		using("upload", function() {
			$("#" + id).upload({
				buttonText: '选择文件',
				iconCls: 'icon-man',
				iconAlign: 'left',
				value: arg,
				maxSize: 1,
				title: "上传图片",
				param: {
					exts: "png,bmp,jpg",
					fileNumLimit: 1
				},
				onClickButton: function() {
					$("#" + id).upload("show");
				}
			});
		})
	}
	var addDialogInit = function(baseUrl) {
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title: config.name + '增加',
			href: baseUrl,
			onLoad: function() {
				combobox.init({
					ele: "#isDefined",
					grouptype: "itemiskey"
				});
				combobox.init({
					ele: "#moudelType",
					grouptype: "oa_power_type"
				});
				combobox.init({
					ele: "#phoneStatus",
					grouptype: "itemiskey"
				});
				using(["combobox"], function() {
					easyutil.ajaxHandler(BASEPATH + '/act/activitiUtil/getAllProcess.htm', null, function(code, msg, data) {
						combobox.init({
							ele: "#processKey",
							valueField: 'key',
							remote: false,
							textField: 'name',
							onSelect: function(rec) {
								$("#processName").val(rec.name);
							}
						}).load(data);
					});
					easyutil.ajaxHandler(BASEPATH + "/base/authoriza/conmonData/queryData.htm", {
						queryId: 'basecode',
						param: 'msg_type'
					}, function(code, msg, data) {
						combobox.init({
							ele: "#msgTypeAdd",
							valueField: 'id',
							remote: false,
							textField: 'text'
						}).load(data);
					});
				});
				initUploadImg('iconUrlAdd');
			},
			buttons: [{
				text: '保存',
				iconCls: 'icon-ok',
				accessKey: {
					akey: 18,
					mkey: 83
				},
				handler: function(row) {
					var num = Number($("#days").val()) * 24 * 60 + Number($("#when").val()) * 60 + Number($("#points").val());
					$("#totalTimeLimitAdd").val(num);
					using(["form"], function() {
						if ($("#addform").form("validate")) {
							easyutil.ajaxHandler(BASEPATH + '/oa/act/actExBuss/saveActExBuss.htm', $("#addform").serializeJson(), function() {
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			}, {
				text: '取消',
				addClass: 'l-btn-red',
				iconCls: 'icon-not',
				accessKey: {
					akey: 18,
					mkey: 67
				},
				handler: function() {
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}
	var updDialogInit = function(baseUrl, row) {
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title: config.name + '修改',
			href: baseUrl,
			onLoad: function() {
				combobox.init({
					ele: "#isDefinedUpd",
					grouptype: "itemiskey"
				});
				combobox.init({
					ele: "#phoneStatusUpd",
					grouptype: "itemiskey"
				});
				combobox.init({
					ele: "#moudelType",
					grouptype: "oa_power_type"
				});
				using(["form"], function() {
					initUploadImg('iconUrlUpd', row.iconUrl);
					using(["combobox"], function() {
						easyutil.ajaxHandler(BASEPATH + "/base/authoriza/conmonData/queryData.htm", {
							queryId: 'basecode',
							param: 'msg_type'
						}, function(code, msg, data) {
							combobox.init({
								ele: "#msgTypeUpd",
								valueField: 'id',
								remote: false,
								textField: 'text'
							}).load(data);
						});
					});
					setTimeout(function() {
						var days = Math.floor(row.totalTimeLimit / 1440);
						var hours = Math.floor((row.totalTimeLimit % 1440) / 60);
						var minutes = Math.floor(((row.totalTimeLimit % 1440) % 60));
						$("#daysUpd").textbox("setValue", days);
						$("#whenUpd").textbox("setValue", hours);
						$("#pointsUpd").textbox("setValue", minutes);
						row.moudelType = row.MOUDEL_TYPE;
						$('#updform').form('load', row);
					}, 100);
				});
			},
			buttons: [{
				text: '保存',
				iconCls: 'icon-ok',
				accessKey: {
					akey: 18,
					mkey: 83
				},
				handler: function() {
					var num2 = Number($("#daysUpd").val()) * 24 * 60 + Number($("#whenUpd").val()) * 60 + Number($("#pointsUpd").val());
					$("#totalTimeLimitUpd").val(num2);
					data = $("#updform").serializeJson();
					data.totalTimeLimit = num2;
					using(["form"], function() {
						if ($("#updform").form("validate")) {
							easyutil.ajaxHandler(BASEPATH + '/oa/act/actExBuss/updateActExBuss.htm', data, function() {
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			}, {
				text: '取消',
				addClass: 'l-btn-red',
				iconCls: 'icon-not',
				accessKey: {
					akey: 18,
					mkey: 67
				},
				handler: function() {
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}

	var delInit = function(baseUrl, row) {
		easyutil.ajaxHandler(baseUrl, {
			businessId: row.businessId
		}, function() {
			dgutil.pager();
		});
	}

	function editProNode(baseUrl, row) {
		var urlTool = require("resource/js/component/urlTool.js");
		var url = baseUrl + "?businessId=" + row.businessId + "&processKey=" + row.processKey + "&buttons=" + urlTool.getParam("buttons");
		backDlg.openBackDialog({
			href: url,
			title: '流程节点配置'
		});
	}

	function importProgress(baseUrl) {

		$("#importdlg").dialog({
			iconCls: 'icon-save',
			title: config.name + '导入',
			href: baseUrl,
			onLoad: function() {
				using("upload", function() {
					$("#processUrlAdd").upload({
						buttonText: '选择文件',
						iconCls: 'icon-man',
						iconAlign: 'left',
						value: "",
						maxSize: 1,
						title: "上传流程",
						param: {
							exts: "xml",
							fileNumLimit: 1
						},
						onClickButton: function() {
							$("#processUrlAdd").upload("show");
						}
					});
				})
			},
			buttons: [{
				text: '保存',
				iconCls: 'icon-ok',
				accessKey: {
					akey: 18,
					mkey: 83
				},
				handler: function(row) {
					using(["form"], function() {
						if ($("#importform").form("validate")) {
							ajax.init().success(function(c, m, d) {
								using("messager", function() {
									$.messager.alert("系统提示", m);
								});
							}).post(BASEPATH + "/oa/act/process/importProcess.htm", {filePath:$("#processUrlAdd").val()});
						}
					});
				}
			}, {
				text: '取消',
				addClass: 'l-btn-red',
				iconCls: 'icon-not',
				accessKey: {
					akey: 18,
					mkey: 67
				},
				handler: function() {
					$('#importdlg').dialog('close');
				}
			}]
		});
		$("#importdlg").dialog("open");


	}
	module.exports = {
		trans: function(id) {
			var buttons = {
				title: {
					_class: "",
					text: "业务流程"
				},
				btn: [
				{
					iconCls: 'icon-add',
					addClass: 'l-btn-display',
					text: "导入工作流",
					url: '/view/oa/act/ex/buss/bussImport',
					handler: importProgress
				},
				{
					iconCls: 'icon-search',
					url: '/oa/act/process/querySysProcess.htm',
					text: "查询",
					addClass: 'l-btn-display',
					handler: function() {
						dgutil.pager(1);
					}
				}, {
					iconCls: 'icon-add',
					addClass: 'l-btn-display',
					text: "添加",
					url: '/view/oa/act/ex/buss/bussAdd',
					handler: addDialogInit
				}, {
					iconCls: 'icon-edit',
					url: '/view/oa/act/ex/buss/bussUpd',
					text: "修改",
					addClass: 'l-btn-display',
					needRow: true,
					handler: updDialogInit
				}, {
					iconCls: 'icon-remove',
					url: '/oa/act/actExBuss/deleteActExBuss.htm',
					text: "删除",
					addClass: 'l-btn-display',
					needRow: true,
					isCheck: true,
					handler: delInit
				}, {
					iconCls: 'icon-icon1_75',
					addClass: 'l-btn-display',
					text: "管理流程节点",
					url: '/view/oa/act/ex/bussDef/def',
					needRow: true,
					handler: editProNode
				}]
			};
			datagridconfig = {
				fitColumns: true,
				singleSelect: true,
				striped: true,
				autoRowHeight: false,
				fit: true,
				url: BASEPATH + '/oa/act/process/querySysProcess.htm',
				toolbar: buttons,
				columns: config.column,
				eid: "dg",
				args: "searchform",
				onDblClickRow: function(index, row) {
					if (row.processKey == undefined || row.processKey == "") return;
					var url = BASEPATH + "/act/activitiImg/getImg.htm?key=" + row.processKey;
					$("#playdlg").dialog({
						title: '图片',
						noheader: true,
						shadow: false,
						content: "<div style='top: 14%;position: absolute;'><img style='width:100%' src='" + url + "'/></div>",
						width: 750,
						height: 400,
						resizable: true
					});
					$(".window-mask").click(function(e) {
						$("#playdlg").dialog("close");
					});
					$("#playdlg").dialog("restore");
					$("#playdlg").dialog("center");
					$("#playdlg").dialog("open");
				}
			};
			using(["datagrid", "parser", "textbox", "dialog", "combobox"], function() {
				initPage();
			});

		}
	}
});
